﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Motar.Desktop.Controls
{

    /// <summary>
    /// 
    /// </summary>
    [TemplateVisualState(Name = StateOn, GroupName = SwitchGroup)]
    [TemplateVisualState(Name = StateOff, GroupName = SwitchGroup)]
    [StyleTypedProperty(Property = "OverlayStyle", StyleTargetType = typeof(Rectangle))]
    public class ViewSwitch : ContentControl
    {

        /// <summary>
        /// 
        /// </summary>
        public const string StateOn = "On";

        /// <summary>
        /// 
        /// </summary>
        public const string StateOff = "Off";

        /// <summary>
        /// 
        /// </summary>
        public const string SwitchGroup = "SwitchStates";

        /// <summary>
        /// 
        /// </summary>
        static ViewSwitch()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ViewSwitch), new FrameworkPropertyMetadata(typeof(ViewSwitch)));
        }

        /// <summary>
        /// 
        /// </summary>
        private readonly DispatcherTimer _displayAfterTimer = new DispatcherTimer();

        /// <summary>
        /// 
        /// </summary>
        public ViewSwitch()
        {
            _displayAfterTimer.Tick += DisplayAfterTimerElapsed;
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty OnContentProperty = DependencyProperty.Register("OnContent", typeof(object), typeof(ViewSwitch), new PropertyMetadata(null));

        /// <summary>
        /// 
        /// </summary>
        public object OnContent
        {
            get { return GetValue(OnContentProperty); }
            set { SetValue(OnContentProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty OnContentTemplateProperty = DependencyProperty.Register("OnContentTemplate", typeof(DataTemplate), typeof(ViewSwitch), new PropertyMetadata(null));

        /// <summary>
        /// 
        /// </summary>
        public DataTemplate OnContentTemplate
        {
            get { return (DataTemplate)GetValue(OnContentTemplateProperty); }
            set { SetValue(OnContentTemplateProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty OverlayStyleProperty = DependencyProperty.Register("OverlayStyle", typeof(Style), typeof(ViewSwitch), new PropertyMetadata(null));

        /// <summary>
        /// 
        /// </summary>
        public Style OverlayStyle
        {
            get { return (Style)GetValue(OverlayStyleProperty); }
            set { SetValue(OverlayStyleProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty IsOnProperty = DependencyProperty.Register("IsOn", typeof(bool), typeof(ViewSwitch), new PropertyMetadata(false, new PropertyChangedCallback(OnIsOnChanged)));

        /// <summary>
        /// 
        /// </summary>
        public bool IsOn
        {
            get { return (bool)GetValue(IsOnProperty); }
            set { SetValue(IsOnProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnIsOnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ViewSwitch)d).OnIsOnChanged(e);
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty DisplayAfterProperty = DependencyProperty.Register("DisplayAfter", typeof(TimeSpan), typeof(ViewSwitch), new PropertyMetadata(TimeSpan.FromSeconds(0.1)));

        /// <summary>
        /// 
        /// </summary>
        public TimeSpan DisplayAfter
        {
            get { return (TimeSpan)GetValue(DisplayAfterProperty); }
            set { SetValue(DisplayAfterProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            ChangeVisualState(false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnIsOnChanged(DependencyPropertyChangedEventArgs e)
        {
            if (IsOn)
            {
                if (!DisplayAfter.Equals(TimeSpan.Zero))
                {
                    _displayAfterTimer.Interval = DisplayAfter;

                    _displayAfterTimer.Start();
                }
            }
            else
            {
                _displayAfterTimer.Stop();
            }

            ChangeVisualState(true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DisplayAfterTimerElapsed(object sender, EventArgs e)
        {
            _displayAfterTimer.Stop();

            ChangeVisualState(true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="useTransitions"></param>
        protected virtual void ChangeVisualState(bool useTransitions)
        {
            VisualStateManager.GoToState(this, IsOn ? StateOn : StateOff, useTransitions);
        }
    }
}
